package com.j2eefast.flowable.erp.controller;

import com.j2eefast.common.core.base.entity.LoginUserEntity;
import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.*;
import com.j2eefast.flowable.bpm.entity.BpmTaskFromEntity;
import com.j2eefast.flowable.bpm.enums.CommentOSSType;
import com.j2eefast.flowable.bpm.service.BpmTaskFromService;
import com.j2eefast.flowable.bpm.utils.CommontOSSUtils;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.system.oss.entity.SysOssEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.ui.ModelMap;

import java.util.List;
import java.util.Map;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.j2eefast.flowable.erp.entity.bpmErpPersonPayEntity;
import com.j2eefast.flowable.erp.service.bpmErpPersonPayService;

/**
 * ERP付款信息页面控制器
 * @author yhli
 * @date 2020-07-16 10:07
 */
@Controller
@RequestMapping("/erp/pay")
public class bpmErpPersonPayController extends BaseController{

    private String prefix = "modules/erp/pay";
    @Autowired
    @Lazy(value = true)
    private CommontOSSUtils commontOSSUtils;
    @Autowired
    private bpmErpPersonPayService bpmErpPersonPayService;
    /**
    * 必须注入实例关联表单服务
    */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;


    @RequiresPermissions("erp:pay:view")
    @GetMapping()
    public String pay(){
        return prefix + "/pay";
    }
        
    @RequestMapping("/list")
    @RequiresPermissions("erp:pay:list")
    @ResponseBody
    public ResponseData list(@RequestParam Map<String, Object> params,bpmErpPersonPayEntity bpmErpPersonPayEntity) {
		PageUtil page = bpmErpPersonPayService.findPage(params,bpmErpPersonPayEntity);
		return success(page);
    }    
            
    @GetMapping("/add")
    public String add(ModelMap mmap){
         bpmErpPersonPayEntity bpmErpPersonPay = new bpmErpPersonPayEntity();
          mmap.put("bpmErpPersonPay", bpmErpPersonPay);
        LoginUserEntity loginInfo=UserUtils.getUserInfo();
        mmap.put("user", loginInfo);
        String serNumber=OrderCodeFactory.generateFKAgain(loginInfo.getId());
        mmap.put("serNumber", serNumber);
        return prefix + "/add";
    }
        /**
         * 定义关联表单申请表单URL对应此处
         */
        @GetMapping("/add/{id}")
        public String add(@PathVariable("id") Long id, ModelMap mmp){
            //通过页面传入的表单ID查询表单关联信息
            BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
            mmp.put("bpmTaskFrom", bpmTaskFrom);
            mmp.put("user", UserUtils.getUserInfo());
            return prefix + "/add";
        }

    /**
     * 新增
     */
    @RepeatSubmit
    @RequiresPermissions("erp:pay:add")
    @BussinessLog(title = "ERP付款信息", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData addbpmErpPersonPay(@Validated bpmErpPersonPayEntity bpmErpPersonPay){
        //校验参数
//        ValidatorUtil.validateEntity(bpmErpPersonPay);
        Long userID=UserUtils.getUserId();
        //初始化数据时将数据设置为未删除
        bpmErpPersonPay.setDeleteFlag("0");
        bpmErpPersonPay.setApplyUserid(userID);
        bpmErpPersonPay.setProcessStatus(1L);
        bpmErpPersonPay.setCreateUserid(userID);
        bpmErpPersonPay.setCreateTime(new Date());
        return bpmErpPersonPayService.savebpmErpPersonPay(bpmErpPersonPay)? success(): error("新增失败!");
    }    
    
    /**
     * 修改
     */
    @RequiresPermissions("erp:pay:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonPayEntity bpmErpPersonPay = bpmErpPersonPayService.getbpmErpPersonPayById(id);
        mmap.put("bpmErpPersonPay", bpmErpPersonPay);
        return prefix + "/edit";
    }
    /**
     * 审批1
     */
    @RequiresPermissions("erp:pay:view1")
    @GetMapping("/view1/{id}")
    public String view1(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonPayEntity bpmErpPersonPay = bpmErpPersonPayService.getbpmErpPersonPayById(id);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPPayReceiptNumberProof.getName(),bpmErpPersonPay.getSerialNumber());
        mmap.put("proof",proof);
        mmap.put("bpmErpPersonPay", bpmErpPersonPay);
        return prefix + "/view1";
    }
    /**
     * 审批2
     */
    @RequiresPermissions("erp:pay:view2")
    @GetMapping("/view2/{id}")
    public String view2(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonPayEntity bpmErpPersonPay = bpmErpPersonPayService.getbpmErpPersonPayById(id);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPPayReceiptNumberProof.getName(),bpmErpPersonPay.getSerialNumber());
        mmap.put("proof",proof);
        mmap.put("bpmErpPersonPay", bpmErpPersonPay);
        return prefix + "/view2";
    }
    /**
     * 修改保存ERP付款信息
     */
    @RepeatSubmit
    @RequiresPermissions("erp:pay:view2")
    @BussinessLog(title = "ERP付款信息", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/view2", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonPayView2(bpmErpPersonPayEntity bpmErpPersonPay){
        bpmErpPersonPay.setProcessStatus(2L);
        Long userID=UserUtils.getUserId();
        bpmErpPersonPay.setUpdateUserid(userID);
        return bpmErpPersonPayService.updatebpmErpPersonPayById(bpmErpPersonPay)? success(): error("修改失败!");
    }
    /**
     * 审批3
     */
    @RequiresPermissions("erp:pay:view3")
    @GetMapping("/view3/{id}")
    public String view3(@PathVariable("id") String id, ModelMap mmap){
        bpmErpPersonPayEntity bpmErpPersonPay = bpmErpPersonPayService.getbpmErpPersonPayById(id);
        List<SysOssEntity> proof = commontOSSUtils.selectListByType(CommentOSSType.ERPPayReceiptNumberProof.getName(),bpmErpPersonPay.getSerialNumber());
        mmap.put("proof",proof);
        mmap.put("bpmErpPersonPay", bpmErpPersonPay);
        return prefix + "/view3";
    }

    /**
     * 修改保存ERP付款信息
     */
    @RepeatSubmit
    @RequiresPermissions("erp:pay:view3")
    @BussinessLog(title = "ERP付款信息", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/view3", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonPayView3(bpmErpPersonPayEntity bpmErpPersonPay){
        bpmErpPersonPay.setProcessStatus(3L);
        Long userID=UserUtils.getUserId();
        bpmErpPersonPay.setUpdateUserid(userID);
        bpmErpPersonPay.setCashierSubmitTime(new Date());
        return bpmErpPersonPayService.updatebpmErpPersonPayById(bpmErpPersonPay)? success(): error("修改失败!");
    }
    /**
     * 修改保存ERP付款信息
     */
    @RepeatSubmit
    @RequiresPermissions("erp:pay:edit")
    @BussinessLog(title = "ERP付款信息", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editbpmErpPersonPay(bpmErpPersonPayEntity bpmErpPersonPay){
//		ValidatorUtil.validateEntity(bpmErpPersonPay);
        return bpmErpPersonPayService.updatebpmErpPersonPayById(bpmErpPersonPay)? success(): error("修改失败!");
    }    

    /**
     * 删除
     */
    @RepeatSubmit
    @BussinessLog(title = "ERP付款信息", businessType = BusinessType.DELETE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @RequiresPermissions("erp:pay:del")
    @ResponseBody
    public ResponseData del(String[] ids) {
      return bpmErpPersonPayService.deletebpmErpPersonPayByIds(ids)? success(): error("删除失败!");
    }    
}
